package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

 @Autowired
 private GoodsService goodsService;

    /**
     *   1. 通过分页查询商品
     * @param page     当前页
     * @param rows     每页大小
     * @param saleable 上架商品或下降商品
     * @param brandId 品牌id
     * @param categoryId 分类id
     * @param id  spu的id
     * @return 当前页商品数据
     */
       @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id) {

        return ResponseEntity.ok(goodsService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }
    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<List<SpuDTO>> addGoods(@RequestBody SpuDTO spuDTO){
        goodsService.addGoods(spuDTO);
           return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<List<SpuDTO>> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *   2. 根据ID批量查询Sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIDs(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.goodsService.listSkuByIDs(ids));
    }

    /**
     *   3. 根据SpuId查询Sku
     * @param pid
     * @return
     */
     @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpuId(@RequestParam("id") Long pid){
         return ResponseEntity.ok(this.goodsService.listSkuBySpuId(pid));
     }

    /**
     *   4. 根据ID查询商品详情
     * @param id
     * @return
     */
     @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> spuDetailById(@RequestParam("id") Long id){
         return ResponseEntity.ok(this.goodsService.spuDetailById(id));
     }

    /**
     *   5. 根据spuId查询spu的所有规格参数值
     * @param pid
     * @param searching
     * @return
     */
     @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpecParamBySpuId(@RequestParam("id")Long pid,
                                                                   @RequestParam(value = "searching",required = false)Boolean searching){

      return ResponseEntity.ok(this.goodsService.listSpecParamBySpuId(pid,searching));
     }

    /**
     *   6. 根据ID查询商品
     * @param id
     * @return
     */
     @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
         return ResponseEntity.ok(this.goodsService.querySpuById(id));
     }
    /**
     *   7. 根据id查询spu及sku、spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){

       return ResponseEntity.ok(this.goodsService.queryGoodsById(id));
    }
    /**
     * 修改商品上下架
     * @param id 商品spu的id
     * @param saleable true：上架；false：下架
     * @return 无
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //@PutMapping("/stock/minus")
    //public ResponseEntity<Void> inReduction(@RequestBody  ){
    //}

}